-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathefficient_string_editor.py
81 lines (68 loc) · 3.01 KB
/
efficient_string_editor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
You have to implement a string editor that starts from empty string and executes sequence of commands:
INSERT some_string – inserts given string at front of the text. Print "OK" as command result.
APPEND some_string – appends given string at the end of the text. Print "OK" as command result.
DELETE start_index count – deletes the specified substring. Print "OK" as command result in case of success. Print "ERROR" in case of invalid substring.
PRINT – prints the string in the editor.
Read the commands from the console, process them and finally print the results. Do not print the results until all commands are processed.
Ensure your programs runs efficiently for tens of thousands of commands.
"""
from rope import Rope
class StringEditor:
PRINT_COMMAND = "PRINT"
APPEND_COMMAND = "APPEND"
DELETE_COMMAND = "DELETE"
INSERT_COMMAND = "INSERT"
AVAILABLE_COMMANDS = [PRINT_COMMAND, APPEND_COMMAND, DELETE_COMMAND, INSERT_COMMAND]
def __init__(self, value=''):
self.content = Rope(value)
self.commands_to_function = {
self.PRINT_COMMAND: self.print_string,
self.INSERT_COMMAND: self.insert_string,
self.DELETE_COMMAND: self.delete_string,
self.APPEND_COMMAND: self.append_string
}
def command_controller(self):
""" Take a command and act on it """
command = input()
while not self._validate_command(command):
print("Command is invalid! Valid commands are:\n\t{}".format('\n\t'.join(self.AVAILABLE_COMMANDS)))
command = input()
arguments = command.split()
command = arguments[0]
args = arguments[1:]
self.commands_to_function[command](args)
def insert_string(self, *args):
""" inserts given string at front of the text. Print "OK" as command result. """
str_to_insert = args[0][0]
self.content.insert(0, str_to_insert)
print('OK')
def append_string(self, *args):
""" append a given string to the end of the text. Print "OK" as command result. """
str_to_append = args[0][0]
self.content.insert(len(self.content), str_to_append)
print('OK')
def delete_string(self, *args):
""" deletes the specified substring. Print "OK" as command result in case of success.
Print "ERROR" in case of invalid substring. """
try:
start = int(args[0][0])
end = start + int(args[0][1])
self.content.remove(start, end)
print('OK')
except:
print('ERROR')
def print_string(self, *args):
print(self.content)
def _validate_command(self, command: str) -> bool:
""" return a boolean indicating if the command is valid """
for valid_command in self.AVAILABLE_COMMANDS:
if command.startswith(valid_command):
return True
return False
def main():
str_editor = StringEditor()
while True:
str_editor.command_controller()
if __name__ == '__main__':
main()